package com.ittx.dzt.baidu.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baidu.dev2.api.sdk.common.ApiRequestHeader;
import com.baidu.dev2.api.sdk.platbusinessorderrefund.api.PlatBusinessOrderRefundService;
import com.baidu.dev2.api.sdk.platbusinessorderrefund.model.*;
import com.ittx.dzt.baidu.handler.AbstractBaseBaiduApiHandler;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.RefundDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 拒绝售后申请
 * <p>
 * 拒绝售后申请 /PlatBusinessOrderRefundService/rejectOrderRefund
 * cy.refund.reject
 * https://dev2.baidu.com/content?sceneType=0&pageId=104172&nodeId=1147&subhead=
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/2/27
 */
@Slf4j
@Service
public class BaiduRefundRejectService extends AbstractBaseBaiduApiHandler<RefundDTO> {
	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.REFUND_REJECT;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, RefundDTO refundDTO) {
		StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
		try {
			Map<String, Object> extendMap = platformApiParamDTO.getExtendMap();
			String authUserName = MapUtil.getStr(extendMap, "authUserName", "");
			if (StrUtil.isEmpty(authUserName)) {
				return Result.error("授权用户名,不可为空,请在拓展参数中加入参数:{}", "authUserName");
			}
			Map<String, Object> headerMap = MapUtil.newHashMap();
			headerMap.put("accessToken", storeConfig.getSessionKey());
			headerMap.put("userName", authUserName);


			Map<String, Object> bodyMap = MapUtil.newHashMap();
			bodyMap.put("appId", "5");
			bodyMap.put("shopId", storeConfig.getStoreUniqueCode());
			bodyMap.put("orderId", refundDTO.getSourceOrderCode());
			bodyMap.put("refundId", refundDTO.getSourceReturnCode());
			bodyMap.put("rejectReason", refundDTO.getRejectReasonCode());
			bodyMap.put("rejectMarks", refundDTO.getRemark());

			Map<String, Object> requestMap = MapUtil.newHashMap();
			requestMap.put("header", headerMap);
			requestMap.put("body", bodyMap);

			return this.executeApi(storeConfig, requestMap, platformApiParamDTO.getRequestId());
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), e.getMessage());
			log.error(msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg));
		}
	}

	@Override
	public String getBaiduMethod() {
		return null;
	}
}
